package per.yuan;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(basePackages = {"per.yuan.openfeign"})
public class Consumer8001 {
    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(Consumer8001.class, args);

    }

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    @RestController
    public class TestController {


        private final RestTemplate restTemplate;

        @Autowired
        public TestController(RestTemplate restTemplate) {
            this.restTemplate = restTemplate;
        }

        @GetMapping
        public String echo(){
            return restTemplate.getForObject("http://nacos-provider-demo/echo",String.class);
        }


    }

    @RestController
    @RefreshScope
    class ConfigController {
        @Value("${user.name}")
        private String name;
        @Value("${user.age}")
        private String age;

        @Value("${info}")
        private String info;
        @GetMapping("/user")
        public String getUser(){
            StringBuilder sb = new StringBuilder();
            sb
                    .append("user: ").append("\n")
                    .append("\tname: " + name).append("\r\n")
                    .append("\tage: " + age).append("\n")
                    .append("info: " + info);
            return sb.toString();
        }
    }
    @RestController
    class SentinelController{

        @GetMapping("/helloSentinel")
        public String hello(){
            return "hello sentinel";
        }

    }




}

